3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
25 use MediaWiki\Block\BlockRestriction
;
26 use MediaWiki\Block\Restriction\Restriction
;
27 use MediaWiki\Block\Restriction\PageRestriction
;
28 use MediaWiki\Block\Restriction\NamespaceRestriction
;
29 use MediaWiki\MediaWikiServices
;
30 use Wikimedia\Rdbms\IResultWrapper
;
32 class BlockListPager
extends TablePager
{
37 * Array of restrictions.
41 protected $restrictions = [];
44 * @param SpecialPage $page
47 public function __construct( $page, $conds ) {
48 $this->conds
= $conds;
49 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
50 parent
::__construct( $page->getContext() );
53 function getFieldNames() {
54 static $headers = null;
56 if ( $headers === null ) {
58 'ipb_timestamp' => 'blocklist-timestamp',
59 'ipb_target' => 'blocklist-target',
60 'ipb_expiry' => 'blocklist-expiry',
61 'ipb_by' => 'blocklist-by',
62 'ipb_params' => 'blocklist-params',
63 'ipb_reason' => 'blocklist-reason',
65 foreach ( $headers as $key => $val ) {
66 $headers[$key] = $this->msg( $val )->text();
73 function formatValue( $name, $value ) {
75 if ( $msg === null ) {
81 'blocklist-nousertalk',
85 'blocklist-editing-sitewide',
88 foreach ( $keys as $key ) {
89 $msg[$key] = $this->msg( $key )->text();
93 /** @var object $row */
94 $row = $this->mCurrentRow
;
96 $language = $this->getLanguage();
100 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
103 case 'ipb_timestamp':
104 $formatted = htmlspecialchars( $language->userTimeAndDate( $value, $this->getUser() ) );
108 if ( $row->ipb_auto
) {
109 $formatted = $this->msg( 'autoblockid', $row->ipb_id
)->parse();
111 list( $target, $type ) = Block
::parseTarget( $row->ipb_address
);
113 case Block
::TYPE_USER
:
115 $formatted = Linker
::userLink( $target->getId(), $target );
116 $formatted .= Linker
::userToolLinks(
120 Linker
::TOOL_LINKS_NOBLOCK
123 case Block
::TYPE_RANGE
:
124 $formatted = htmlspecialchars( $target );
130 $formatted = htmlspecialchars( $language->formatExpiry(
132 /* User preference timezone */true
134 if ( $this->getUser()->isAllowed( 'block' ) ) {
135 if ( $row->ipb_auto
) {
136 $links[] = $linkRenderer->makeKnownLink(
137 SpecialPage
::getTitleFor( 'Unblock' ),
140 [ 'wpTarget' => "#{$row->ipb_id}" ]
143 $links[] = $linkRenderer->makeKnownLink(
144 SpecialPage
::getTitleFor( 'Unblock', $row->ipb_address
),
147 $links[] = $linkRenderer->makeKnownLink(
148 SpecialPage
::getTitleFor( 'Block', $row->ipb_address
),
149 $msg['change-blocklink']
152 $formatted .= ' ' . Html
::rawElement(
154 [ 'class' => 'mw-blocklist-actions' ],
155 $this->msg( 'parentheses' )->rawParams(
156 $language->pipeList( $links ) )->escaped()
159 if ( $value !== 'infinity' ) {
160 $timestamp = new MWTimestamp( $value );
161 $formatted .= '<br />' . $this->msg(
162 'ipb-blocklist-duration-left',
163 $language->formatDuration(
164 $timestamp->getTimestamp() - MWTimestamp
::time(),
178 if ( isset( $row->by_user_name
) ) {
179 $formatted = Linker
::userLink( $value, $row->by_user_name
);
180 $formatted .= Linker
::userToolLinks( $value, $row->by_user_name
);
182 $formatted = htmlspecialchars( $row->ipb_by_text
); // foreign user?
187 $value = CommentStore
::getStore()->getComment( 'ipb_reason', $row )->text
;
188 $formatted = Linker
::formatComment( $value );
194 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) && $row->ipb_sitewide
) {
195 $properties[] = htmlspecialchars( $msg['blocklist-editing-sitewide'] );
198 if ( !$row->ipb_sitewide
&& $this->restrictions
) {
199 $list = $this->getRestrictionListHTML( $row );
201 $properties[] = htmlspecialchars( $msg['blocklist-editing'] ) . $list;
205 if ( $row->ipb_anon_only
) {
206 $properties[] = htmlspecialchars( $msg['anononlyblock'] );
208 if ( $row->ipb_create_account
) {
209 $properties[] = htmlspecialchars( $msg['createaccountblock'] );
211 if ( $row->ipb_user
&& !$row->ipb_enable_autoblock
) {
212 $properties[] = htmlspecialchars( $msg['noautoblockblock'] );
215 if ( $row->ipb_block_email
) {
216 $properties[] = htmlspecialchars( $msg['emailblock'] );
219 if ( !$row->ipb_allow_usertalk
) {
220 $properties[] = htmlspecialchars( $msg['blocklist-nousertalk'] );
223 $formatted = Html
::rawElement(
226 implode( '', array_map( function ( $prop ) {
227 return Html
::rawElement(
237 $formatted = "Unable to format $name";
245 * Get Restriction List HTML
247 * @param stdClass $row
251 private function getRestrictionListHTML( stdClass
$row ) {
254 foreach ( $this->restrictions
as $restriction ) {
255 if ( $restriction->getBlockId() !== (int)$row->ipb_id
) {
259 switch ( $restriction->getType() ) {
260 case PageRestriction
::TYPE
:
261 if ( $restriction->getTitle() ) {
262 $items[$restriction->getType()][] = Html
::rawElement(
265 Linker
::link( $restriction->getTitle() )
269 case NamespaceRestriction
::TYPE
:
270 $text = $restriction->getValue() === NS_MAIN
271 ?
$this->msg( 'blanknamespace' )
272 : $this->getLanguage()->getFormattedNsText(
273 $restriction->getValue()
275 $items[$restriction->getType()][] = Html
::rawElement(
279 SpecialPage
::getTitleValueFor( 'Allpages' ),
283 'namespace' => $restriction->getValue()
291 if ( empty( $items ) ) {
296 foreach ( $items as $key => $value ) {
297 $sets[] = Html
::rawElement(
300 $this->msg( 'blocklist-editing-' . $key ) . Html
::rawElement(
303 implode( '', $value )
308 return Html
::rawElement(
315 function getQueryInfo() {
316 $commentQuery = CommentStore
::getStore()->getJoin( 'ipb_reason' );
317 $actorQuery = ActorMigration
::newMigration()->getJoin( 'ipb_by' );
320 'tables' => array_merge(
321 [ 'ipblocks' ], $commentQuery['tables'], $actorQuery['tables'], [ 'user' ]
327 'by_user_name' => 'user_name',
331 'ipb_create_account',
332 'ipb_enable_autoblock',
338 'ipb_allow_usertalk',
340 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
341 'conds' => $this->conds
,
343 'user' => [ 'LEFT JOIN', 'user_id = ' . $actorQuery['fields']['ipb_by'] ]
344 ] +
$commentQuery['joins'] +
$actorQuery['joins']
347 # Filter out any expired blocks
348 $db = $this->getDatabase();
349 $info['conds'][] = 'ipb_expiry > ' . $db->addQuotes( $db->timestamp() );
351 # Is the user allowed to see hidden blocks?
352 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
353 $info['conds']['ipb_deleted'] = 0;
360 * Get total number of autoblocks at any given time
362 * @return int Total number of unexpired active autoblocks
364 function getTotalAutoblocks() {
365 $dbr = $this->getDatabase();
366 $res = $dbr->selectField( 'ipblocks',
367 [ 'COUNT(*) AS totalautoblocks' ],
370 'ipb_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
376 return 0; // We found nothing
379 protected function getTableClass() {
380 return parent
::getTableClass() . ' mw-blocklist';
383 function getIndexField() {
384 return 'ipb_timestamp';
387 function getDefaultSort() {
388 return 'ipb_timestamp';
391 function isFieldSortable( $name ) {
396 * Do a LinkBatch query to minimise database load when generating all these links
397 * @param IResultWrapper $result
399 function preprocessResults( $result ) {
400 # Do a link batch query
402 $lb->setCaller( __METHOD__
);
405 foreach ( $result as $row ) {
406 $lb->add( NS_USER
, $row->ipb_address
);
407 $lb->add( NS_USER_TALK
, $row->ipb_address
);
409 if ( isset( $row->by_user_name
) ) {
410 $lb->add( NS_USER
, $row->by_user_name
);
411 $lb->add( NS_USER_TALK
, $row->by_user_name
);
414 if ( !$row->ipb_sitewide
) {
415 $partialBlocks[] = $row->ipb_id
;
419 if ( $partialBlocks ) {
420 // Mutations to the $row object are not persisted. The restrictions will
421 // need be stored in a separate store.
422 $this->restrictions
= BlockRestriction
::loadByBlockId( $partialBlocks );